<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class NewsCateSeeder extends Seeder
{
    // php artisan db:seed --class=NewsCateSeeder

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 清空数据表 后在添加
        $time = time();
        $toDb = DB::table('news_cate');
        $toDb->truncate();

        // 查询原来的数据
        $sql   = "select object_id, term_taxonomy_id
            from wp_term_relationships
            where term_taxonomy_id in (select id from apt_site_cate);";
        $cates = DB::select($sql);
        $cates = json_decode(json_encode($cates), true);

        // 整理数据
        $all = [];
        foreach($cates as $_cate){
            $all[] = $this->makeRow($_cate, $time);
        }

        // 执行
        $toDb->insert($all);
    }


    // 生成数据
    protected function makeRow($_cate, $time)
    {
        $cate_id = $_cate['term_taxonomy_id'];
        if(in_array($cate_id, [4, 1825, 1827])){
            $cate_id = 7;
        }


        $_row = [
            //'id'       => $_tag['term_id'],
            'news_id' => $_cate['object_id'],
            'cate_id' => $cate_id,
            'ctime'   => $time,
        ];

        return $_row;
    }

}
